package com.leoman.spinningmachine.tab3;

import android.graphics.Paint;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.leoman.helper.util.AppManager;
import com.leoman.helper.util.LogUtil;
import com.leoman.helper.util.ToastUtil;
import com.leoman.spinningmachine.R;
import com.leoman.spinningmachine.base.BaseActivity;
import com.leoman.spinningmachine.constant.Constant;
import com.leoman.spinningmachine.http.APIResponse;
import com.leoman.spinningmachine.http.WebServiceApi;
import com.leoman.spinningmachine.login.LoginActivity;
import com.leoman.spinningmachine.util.DialogUtils;
import com.leoman.spinningmachine.util.SharedPreferencesUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018-05-07.
 */
public class UpPwdActivity extends BaseActivity{

    @BindView(R.id.tv_qh)
    TextView tv_qh;

    @BindView(R.id.et_phone)
    EditText et_phone;
    @BindView(R.id.tv_get_code)
    TextView tv_get_code;
    @BindView(R.id.et_code)
    EditText et_code;
    @BindView(R.id.et_new_pwd)
    EditText et_new_pwd;
    private CountDownTimer timer;
    private String phone;
    private String code;
    private String newpwd;

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.ac_uppwd;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        initTitle("修改密码");
        tv_qh.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);//下划线

    }

    @Override
    protected void initData() {

    }
    @OnClick({R.id.tv_get_code,R.id.btn_submit})
    public void onClicked(View view){
        switch (view.getId()){
            case R.id.tv_get_code:
                getCode();
                break;
            case R.id.btn_submit:
                submit();
                break;
        }
    }

    private void submit() {
        phone = et_phone.getText().toString().trim();
        code = et_code.getText().toString().trim();
        newpwd = et_new_pwd.getText().toString();
        if(TextUtils.isEmpty(phone)){
            DialogUtils.getInstance().showResultDialog(this,"请输入手机号");
        }else if(TextUtils.isEmpty(code)){
            DialogUtils.getInstance().showResultDialog(this,"请输入验证码");
        }else if(TextUtils.isEmpty(newpwd)){
            DialogUtils.getInstance().showResultDialog(this,"请输入新密码");
        } else {
            DialogUtils.getInstance().show(this);
            WebServiceApi.getInstance().upPwd(phone,code,newpwd,newpwd,this,2);
        }
    }

    private void getCode() {
        phone = et_phone.getText().toString().trim();
        if(TextUtils.isEmpty(phone)){
            DialogUtils.getInstance().showResultDialog(this,"请输入手机号");
        }else {
            sentVetify(tv_get_code);
            DialogUtils.getInstance().show(this);
            WebServiceApi.getInstance().sendcodeRequest(phone,"findpwd",this,1);
        }
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void OnSuccessData(APIResponse apiResponse, Integer tag) {
        DialogUtils.getInstance().cancel();
        if(1 == tag){
            et_code.setText(""+apiResponse.data.code);
            LogUtil.json("验证码："+apiResponse.data.code);
        }
        if(2 == tag){
            AppManager.getInstance().finishAllActivity();
            SharedPreferencesUtils.getInstance().remove("token");
            SharedPreferencesUtils.getInstance().remove(Constant.ISLOGIN);
            ToastUtil.showToast(this,apiResponse.message, Toast.LENGTH_SHORT);
            gotoOtherActivity(LoginActivity.class);
            finish();
        }
    }
}
